package com.txw.travel.controller.backstage;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.txw.travel.service.PermissionService;
import com.txw.travel.pojo.Permission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/backstage/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/all")
    @PreAuthorize("hasAnyAuthority('/permission/all')")
    public ModelAndView all(@RequestParam(defaultValue = "1") int index, @RequestParam(defaultValue = "3") int size) {
        Page<Permission> permissionPage = permissionService.getPermissionList(index, size);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("permissionPage", permissionPage);
        modelAndView.setViewName("/backstage/permission_all");
        return modelAndView;
    }

    @RequestMapping("/add")
    public String add(Permission permission) {
        permissionService.insertPermission(permission);
        return "redirect:/backstage/permission/all";
    }

    @RequestMapping("/edit")
    public ModelAndView edit(Integer pid) {
        Permission permission = permissionService.getPermissionById(pid);
        if (permission == null)
            return new ModelAndView("redirect:/backstage/permission/all");

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("permission", permission);
        modelAndView.setViewName("/backstage/permission_edit");
        return modelAndView;
    }

    @RequestMapping("/update")
    public String edit(Permission permission) {
        permissionService.updatePermission(permission);
        return "redirect:/backstage/permission/all";
    }

    @RequestMapping("/delete")
    public String delete(Integer pid) {
        permissionService.deletePermission(pid);
        return "redirect:/backstage/permission/all";
    }
}
